package com.gph.etrip;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.gph.etrip.timmy.test.AndroidSaxXMLMarkerParser;
import com.gph.etrip.timmy.test.Marker;
import com.gph.etrip.timmy.test.ViewAdapter;

public class RouteList extends Activity {


			    ListView list;
			    ViewAdapter adapter;
//			    String[] mStrings;
			
			    ArrayList<Marker> markerList = new ArrayList<Marker>();
			    private String selectedMarker = null;
			    private String markerUrl= null;
			    private String category= null;
			    @Override
			    public void onCreate(Bundle savedInstanceState) {
			        super.onCreate(savedInstanceState);
			        setContentView(R.layout.main);
					Intent intent = getIntent();
					category = intent.getStringExtra("cate2route");
			        
			        try {
			        	
						markerList = getMarkerList();

						
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			        
			        list=(ListView)findViewById(R.id.list);
			        adapter=new ViewAdapter(this,markerList);
			        list.setAdapter(adapter);
	
			        
			        final DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			        	  
			    		  public void onClick(DialogInterface dialog, int which) {
			    		        switch (which){
			    		        case DialogInterface.BUTTON_POSITIVE:
			    		        	
			    		        	//Toast.makeText(getApplicationContext(), "11111", Toast.LENGTH_LONG).show();
			    		        	
//			    		    		Intent intent= new Intent();
//			    					intent.putExtra("testIntent", selectedMarker);
//			    					intent.setClass(RewardsListView.this,SingleMarkerView.class);
//			    					RewardsListView.this.startActivity(intent);
			    		            break;
			    		            
			    		        case DialogInterface.BUTTON_NEGATIVE:
			    		            //No button clicked
			    		            break;
			    		        }
			    		    }
			    		};
			    		list.setOnItemClickListener(new OnItemClickListener() {

							public void onItemClick(AdapterView<?> parent, View arg1,
									int position, long arg3) {
								selectedMarker = (String) adapter.getMarkerName(position);
								markerUrl = (String) adapter.getMarkerIcon(position);

								Intent intent = new Intent(RouteList.this,ActivityList.class);
								RouteList.this.startActivity(intent);			
								
								/*
					    		AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());
					    		builder.setMessage("Marker Type?").setPositiveButton("Current Location", dialogClickListener)
					    		    .setNegativeButton("With Distance", dialogClickListener).show();	
				    		    */
							}
			        	  });
			    }
			    
			    @Override
			    public void onDestroy()
			    {
			        adapter.imageLoader.stopThread();
			        list.setAdapter(null);
			        super.onDestroy();
			    }
			    
			    public OnClickListener listener=new OnClickListener(){
			        public void onClick(View arg0) {
			            adapter.imageLoader.clearCache();
			            adapter.notifyDataSetChanged();
			        }
			    };
			    
			    /**
			     * Retrieve defined markers provided in markerconfig.xml, in the assets folder.
			     * @return ArrayList of markers found in the configuration file.
			     */
			    private ArrayList<Marker> getMarkerList() throws FileNotFoundException {
			    	AndroidSaxXMLMarkerParser markerParser;
					try {
						markerParser = new AndroidSaxXMLMarkerParser();
						return markerParser.parse(getAssets().open("routelist.xml"), category);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    	return null; 

				}
			    
		
			

		
		
	}

